unit FastIntLog2;
(*
 *  Very fast integer Log base 2 functions
 *
 *  Author: Emil M. Santos (ems@codexterity.com)
 *)

interface

  function IntLog2( Value: integer ): integer;
  function IntLog2E( Value: integer ): integer;

implementation
uses SysUtils;


 (* no error checking *)
 function IntLog2( Value: integer ): integer;
 asm
    BSR EAX, EAX
 end;


 (* checks for 0 *)
 function IntLog2E( Value: integer ): integer;
 const TExceptClass: TClass = EInvalidOp;
       Msg: string = 'Invalid input to logarithm function.';
 asm
    CMP EAX, 0
    JG @@Ok

    MOV DL,1
    MOV EAX, TExceptClass
    MOV ECX, Msg
    CALL Exception.Create
    CALL System.@RaiseExcept

    RET
 @@Ok:
    BSR EAX, EAX
 end;


end.
